package udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Date;

public class TesteClientSocketUDP extends Thread {

    private DatagramSocket sock;
    private InetAddress host;
    private byte[] buffer = new byte[28];
    private int port;

    public TesteClientSocketUDP(String host, int port) {
        try {
            this.host = InetAddress.getByName(host);
        } catch (Exception e) {
        }
        this.port = port;
    }

    public void run() {
        boolean eof = true;
        DatagramPacket dp = new DatagramPacket(buffer, 28, host, port);

        while (eof) {
            try {
                sock = new DatagramSocket();
                sock.connect(host, port);

                Date data = new Date();

                for (int i = 0; i < data.toString().toCharArray().length; i++) {
                    buffer[i] = (byte) data.toString().toCharArray()[i];
                }

                sock.send(dp);

                Thread.sleep(2000);

            } catch (Exception e) {
                System.out.println("Erro de IO:" + e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        TesteClientSocketUDP client = new TesteClientSocketUDP("127.0.0.1", 4413);
        client.start();
    }
}
